package de.tsajar.es.input.devices;

import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;

/**
 *
 * @author Eike Foede
 */
public class KeyBoardDevice extends AbstractAppState implements InputDevice, ActionListener {

    private Camera cam;
    private Vector3f walkDirection = new Vector3f();
    private boolean jump = false, left = false, right = false, front = false, back = false, up = false, down = false, physical = true;//Ob er in die Richtung läuft, wird benutzt, um die Werte vom OnAction()-Ereignis zum update() zu übergeben
    private boolean running = false;
    float runFac = 4f; //Wieviel schneller Rennen als normal Laufen ist

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.cam = app.getCamera();

        app.getInputManager().addMapping("RUN", new KeyTrigger(KeyInput.KEY_LSHIFT));
        app.getInputManager().addMapping("JUMP", new KeyTrigger(KeyInput.KEY_SPACE));
        app.getInputManager().addMapping("LEFT", new KeyTrigger(KeyInput.KEY_A));
        app.getInputManager().addMapping("RIGHT", new KeyTrigger(KeyInput.KEY_D));
        app.getInputManager().addMapping("FORWARD", new KeyTrigger(KeyInput.KEY_W));
        app.getInputManager().addMapping("BACKWARD", new KeyTrigger(KeyInput.KEY_S));
        app.getInputManager().addListener(this, new String[]{
            "FORWARD",
            "BACKWARD",
            "LEFT",
            "RIGHT",
            "RUN",
            "JUMP"});
    }

    @Override
    public void update(float tpf) {
        Vector3f camDir = cam.getDirection().clone();//.multLocal(0.6f);
        Vector3f camLeft = cam.getLeft().clone();//.multLocal(0.4f);
        walkDirection.set(0, 0, 0);
        if (left) {
            walkDirection.addLocal(camLeft);
        }
        if (right) {
            walkDirection.addLocal(camLeft.negate());
        }
        if (front) {
            walkDirection.addLocal(camDir);
        }
        if (back) {
            walkDirection.addLocal(camDir.negate());
        }
        if (running) {
            walkDirection.multLocal(runFac);
        }
//        walkDirection.multLocal(0.2f);
//        System.out.println(es.size());
        walkDirection.multLocal(new Vector3f(1, 0, 1));
    }

    public void onAction(String binding, boolean value, float tpf) {
        if (binding.equals("LEFT")) {
            left = value;
        } else if (binding.equals("RIGHT")) {
            right = value;
        } else if (binding.equals("FORWARD")) {
            front = value;
        } else if (binding.equals("BACKWARD")) {
            back = value;
        } else if (binding.equals("RUN") && value) {
            running = !running;
        } else if (binding.equals("JUMP") && value) {
            jump = true;
        }
    }

    @Override
    public Vector3f getWalkDirection() {
        return walkDirection;
    }

    @Override
    public boolean getJumping() {
        boolean ret = jump;
        jump = false;
        return ret;
    }
}
